#include <stdlib.h>
#include <exl.h>
#include <queue>
#include <string>
#include <sstream>
#include <map>
#include <fstream>
#include "VoiceRecognizer.h"
#include "Recorder.h"
#include "CppRobot.h"
#include "AccelerometerHandler.h"
#include "CommandHandler.h"


// Pode ignorar as 3 linhas a seguir
//extern int continuous_rec();
//extern int each_word_vr();
//extern int old_vr();


// Funcao principal
int recognition_boladona(const std::string & modelConfigFile, const std::string & keysConfigFile, const std::string & logFile)
{
	// Gravacao continua
	int bufferTime_ms = 25;
	int numOfBuffers  = 20;

	CommandHandler commandHandler(keysConfigFile);
	commandHandler.start();

	VoiceRecognizer recog(modelConfigFile, logFile, bufferTime_ms);
	recog.setCommandHandler(commandHandler);
	recog.start();

	Recorder rec(bufferTime_ms, numOfBuffers);
	rec.setSoundBufferConsumer(recog);
	rec.start();

	AccelerometerHandler accel;
	accel.setCommandHandler(commandHandler);
	accel.start();

	// Aguarda usuario digitar Escape
	while (!GetAsyncKeyState(VK_ESCAPE)) Sleep(500);

	// Espera threads terminarem
	rec.tryToTerminate();
	recog.tryToTerminate();
	accel.tryToTerminate();
	commandHandler.tryToTerminate();


	rec.join();
	recog.join();
	accel.join();
	commandHandler.join();


	return 0;
}



int main(int argc, char * argv [])
{
	Sleep(2500);

	if (argc < 3)
	{
		std::cout << "Uso: JoyCap model-config keys-config [log-file]" << std::endl;
		exit(1);
	}

	int result = recognition_boladona(argv[1], argv[2], argc > 3 ? argv[3] : "");

	//int result = continuous_rec();
	//int result = each_word_vr();
	//int result = old_vr();

	//class InputThread : public exl::ps::ThreadBase {
	//public:
	//	int run()
	//	{
	//		int keys[] = { ' ', VK_LEFT, VK_RIGHT };
	//		Sleep(1500);
	//		while (!mustTerminate())
	//		{    
	//			INPUT input;
	//			memset(&input, 0, sizeof(INPUT));
	//			input.type = INPUT_KEYBOARD;
	//			input.ki.wVk = keys[rand()%3];

	//			std::cout << SendInput(1, &input, sizeof(INPUT)) << std::endl;
	//			Sleep(500);
	//		}

	//		return 0;
	//	}
	//} ith;

	//int result = 0;
	////ith.start();
	////std::cin >> result;


	system("PAUSE");
	return result;
}
